import math

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
from dash import html, dcc

class DashUtil():
    def file_draw(file_path):
        df = pd.read_excel(file_path, sheet_name=0)
        fig = go.Figure()                           # 声明fig为一个图表
        fig.add_trace(go.Scatter(                   # 通过add_trace，往图表中加入折线。折线是go.Scatter类
            name="粮食产量",
            x = df.iloc[:, 0],
            y = df.iloc[:, 1],
            mode = "markers+lines+text",
            text = df.iloc[:, 1],
            textposition="top center"
        ))

        # df = pd.DataFrame({'x': [1, 2, 3], 'SF': [4, 1, 2], 'Montreal': [2, 4, 5]})  # 原始数据
        # fig = px.bar(df, x='x', y=['SF', 'Montreal'], barmode='group')  # 柱状图
        # 我也可以指定输入参数和输出参数，这样就能按照我的意愿绘制图形了
        '''
        month_list = [i + 1 for i in range(12)]
        math_list = np.random.randint(0, 100, 12)
        chinese_list = np.random.randint(0, 100, 12)
        english_list = np.random.randint(0, 100, 12)
        print("考试月份：", month_list)
        print("数学分数：", math_list)
        print("语文分数：", chinese_list)
        print("英语分数：", english_list)

        fig.add_trace(go.Scatter(  # 通过add_trace，往图表中加入折线。折线是go.Scatter类
            name="Math",
            x=month_list,  # 声明scatter的x轴数据，月份
            y=math_list,  # 声明scatter的y轴数据，成绩
            mode="markers+lines+text",  # mode设置了折线图显示:lines折线、markers点、text 数据可以任意组合，使用加号连接
            text=math_list,
            textposition="top center"
        ))
        fig.add_trace(go.Scatter(  # 通过add_trace，往图表中加入折线。折线是go.Scatter类
            name="Chinese",
            x=month_list,  # 声明scatter的x轴数据，月份
            y=chinese_list,  # 声明scatter的y轴数据，成绩
            mode="markers+lines+text",  # mode设置了折线图显示:lines折线、markers点、text 数据可以任意组合，使用加号连接
            text=chinese_list,
            textposition="top center"
        ))
        fig.add_trace(go.Scatter(  # 通过add_trace，往图表中加入折线。折线是go.Scatter类
            name="English",
            x=month_list,  # 声明scatter的x轴数据，月份
            y=english_list,  # 声明scatter的y轴数据，成绩
            mode="markers+lines+text",  # mode设置了折线图显示:lines折线、markers点、text 数据可以任意组合，使用加号连接
            text=english_list,
            textposition="top center"
        ))
        '''

        fig.update_layout(
            title="粮食产量",
            xaxis=dict(title = "产量", nticks = df.iloc[:, 0].size + 1),                 # nticks 设置了要画多少个刻度线，0也是有刻度的
            yaxis=dict(title = "国家",
                        nticks = math.ceil(df.iloc[:, 1].max() / 1000) + 1,
                        rangemode = "tozero",
                        range=(0, (df.iloc[:, 1].max()) + 1000)),                         # rangemode设置使y轴的0刻度对其最下侧
            width=800,
            height=500,
            template="simple_white"
        )

        return html.Div(children=[
            html.H1(children='Hello Dash'),
            html.Div(children='Dash: 一款Python web应用框架'),
            dcc.Graph(id='example-graph', figure=fig)
        ])